package com.wuhan.template.module.find.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.base.BasePageAdapter;
import com.wuhan.template.module.find.bean.WenzhengBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
public class WenzhengPagerAdapter extends BasePageAdapter {

    private List<WenzhengBean.ContentBean.ChildsDataBean> list;
    private List<ImageView> imageViews = new ArrayList<>();
    private Context mContext;
    private final DisplayImageOptions options;


    public WenzhengPagerAdapter(List<WenzhengBean.ContentBean.ChildsDataBean> list, Context context) {
        this.list = list;
        this.mContext = context;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                        //.displayer(new RoundedBitmapDisplayer(20))
                .build();
        for (int i = 0; i < list.size(); i++) {
            imageViews.add(new ImageView(mContext));
        }
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        position %= list.size();
        if (position < 0) {
            position = list.size() + position;
        }
        String url = list.get(position).getHost()
                + list.get(position).getDir()
                + list.get(position).getFilepath()
                + list.get(position).getFilename();
        ImageView currentImg = imageViews.get(position);
        currentImg.setScaleType(ImageView.ScaleType.FIT_XY);
        ((BaseApplication) mContext.getApplicationContext())
                .getImageLoader()
                .displayImage(url, currentImg, options);
        ViewParent vp = currentImg.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(currentImg);
        }
        container.addView(currentImg);

        return currentImg;

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
